package com.xsoft.framework.util;

public class Tohtml {

	public static String TextToHtml(String sourcestr) {
		sourcestr=foo(sourcestr);
		int strlen;
		StringBuffer restring = new StringBuffer();
		String destr = "";
		strlen = sourcestr.length();
		char ch;
		int index = 0;
		for (int i = 0; i < strlen; i++) {
			ch = sourcestr.charAt(i);
			switch (ch) {
			case '<':
				destr = "<";
				index = 1;
				break;
			case '>':
				destr = ">";
				index = 0;
				break;
			case '\"':
				destr = "";
				break;
			case '&':
				destr = "&";
				break;
			case 13:
				destr = "<br>";
				break;
			case 32:
				if (index == 0) {
					destr = "&nbsp;";
				} else {
					destr = " ";
				}
				break;
			case 12288:
				destr = "<p>";
				break;
			default:
				destr = "" + ch;
				break;
			}
			restring.append(destr);
			destr = "";
		}
		return restring.toString();
	}

	public static String TextToHtml2(String sourcestr) {
		int strlen;
		StringBuffer restring = new StringBuffer();
		String destr = "";
		strlen = sourcestr.length();
		char ch;
		int index = 0;
		for (int i = 0; i < strlen; i++) {
			ch = sourcestr.charAt(i);
			switch (ch) {
			case '<':
				destr = "<";
				index = 1;
				break;
			case '>':
				destr = ">";
				index = 0;
				break;
			case '\"':
				destr = "";
				break;
			case '&':
				destr = "&";
				break;
			case 13:
				destr = "<br>";
				break;
			case 32:
				if (index == 0) {
					destr = "&nbsp;";
				} else {
					destr = " ";
				}
				break;
//			case 12288:
//				destr = "<p>";
//				break;
			default:
				destr = "" + ch;
				break;
			}
			restring.append(destr);
			destr = "";
		}
		return restring.toString();
	}
	
	private static String foo(String s){
		if(s==null){
			return "";
		}
		s=s.replaceAll("\\<.*?\\>","");
		s=s.replaceAll("    "," ");
		s=s.replaceAll("   "," ");
		s=s.replaceAll("  "," ");
		s=s.replaceAll("　","");
		s=s.replaceAll("	","");
		s=s.replaceAll("&nbsp;","");
		s=s.replaceAll("\r\n\r\n\r\n\r\n\r\n","\r\n");
		s=s.replaceAll("\r\n\r\n\r\n\r\n","\r\n");
		s=s.replaceAll("\r\n\r\n\r\n","\r\n");
		s=s.replaceAll("\r\n\r\n","\r\n");
		s=s.replace("\r\n","<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;");
		s="&nbsp;&nbsp;&nbsp;&nbsp;"+s;

		return s;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	public static void main(String[] args) {
		// String s = "中国证监会年监管信息";

		StringBuffer sb = new StringBuffer();

		sb.append("<p class='MsoNormal' style='margin: 0cm 0cm 0pt; text-indent: 24pt; mso-char-indent-count: 2.0'><span style='font-size: 12pt; font-family: 仿宋_GB2312'>指定交易这一概念是针对上海证券市场的交易方式而言的。所谓指定交易，是指投资者可以指定某一证券营业部为自己买卖证券的唯一的交易营业部。在与该证券营业部签订协议并完成一定的登记程序后，投资者便可以通过指定的证券营业部进行委托、交易、结算、查询以及享有其他市场服务。<span lang='EN-US'> <br />");
		sb.append("<span style='mso-spacerun: yes'>&nbsp;&nbsp;&nbsp; </span></span>撤销指定交易是相对指定交易而言的，投资若转到其他券商处去，就必须先在原开户的券商处办理撤销指定的手续，再在新的开户券商处办理指定交易。<span lang='EN-US'><o:p></o:p></span></span></p>");

		// for (int i = 0; i < 10000; i++) {
		// sb.append(s);
		// }
		//
		// long startTime = System.currentTimeMillis();
		//
		// TextToHtml_bak(sb.toString());
		//
		// long elapsedTime = System.currentTimeMillis() - startTime;
		//
		// System.out.println(elapsedTime);
		//
		// long startTime1 = System.currentTimeMillis();

		System.out.println(foo(sb.toString()));

		// long elapsedTime1 = System.currentTimeMillis() - startTime1;
		//
		// System.out.println(elapsedTime1);

	}
}
